package com;

import com.lagou.bean.ConsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class AnnotationConsumerMain {
    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);
        LinkedBlockingDeque linkedBlockingDeque = new LinkedBlockingDeque();
        Executor executor = new ThreadPoolExecutor(1,20,2000L,
                TimeUnit.MILLISECONDS,linkedBlockingDeque);


        int total = 3000;
        int init = 0;
        while (init < total) {
            init++;

            System.out.println(init);
            executor.execute(() ->{
                String hello1 = service.sayHello1("hello1");
                System.out.println("result:"+hello1);
            });

            executor.execute(() ->{
                String hello2 = service.sayHello2("hello2");
                System.out.println("result:"+hello2);
            });

            executor.execute(() ->{
                String hello3 = service.sayHello1("hello3");
                System.out.println("result:"+hello3);
            });
        }
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan("com.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
